package seven_day;

/**
 * 将一个字符串从左开始前k个字符进行旋转：左旋数组 。例如字符串："abcdef"，前2个左旋结果是："cdefab"
 */
public class LeftRotateString {
    /**
     * 拼接字符串
     * @param str
     * @param k
     */
    public static void leftRotateString1(String str, int k) {
        if (str == null || str.length() == 0 || k == 0) {
            System.out.println(str);
            return;
        }
        char[] arr = str.toCharArray();
        StringBuilder s = new StringBuilder(str);
        System.out.println(s.substring(k) + s.substring(0, k));
    }

    /**
     * 三段翻转逆置法
     * @param str
     * @param k
     */
    public static void leftRotateString2(String str, int k) {
        if (str == null || str.length() == 0 || k == 0) {
            System.out.println(str);
            return;
        }
        StringBuilder s = new StringBuilder(str);
        StringBuilder s1 = s.delete(0, k);
        s1.reverse().append(s).reverse();
        System.out.println(s1.toString());
    }

    public static void main(String[] args) {
        String str = "abcdefg";
        leftRotateString1(str, 4);
    }
}
